function varargout = uDWC_Test_Stand_Control(varargin)
%UDWC_TEST_STAND_CONTROL M-file for uDWC_Test_Stand_Control.fig
%      UDWC_TEST_STAND_CONTROL, by itself, creates a new UDWC_TEST_STAND_CONTROL or raises the existing
%      singleton*.
%
%      H = UDWC_TEST_STAND_CONTROL returns the handle to a new UDWC_TEST_STAND_CONTROL or the handle to
%      the existing singleton*.
%
%      UDWC_TEST_STAND_CONTROL('Property','Value',...) creates a new UDWC_TEST_STAND_CONTROL using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to uDWC_Test_Stand_Control_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      UDWC_TEST_STAND_CONTROL('CALLBACK') and UDWC_TEST_STAND_CONTROL('CALLBACK',hObject,...) call the
%      local function named CALLBACK in UDWC_TEST_STAND_CONTROL.M with the given input
%      arguments.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help uDWC_Test_Stand_Control

% Last Modified by GUIDE v2.5 28-Jul-2012 01:55:29

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @uDWC_Test_Stand_Control_OpeningFcn, ...
                   'gui_OutputFcn',  @uDWC_Test_Stand_Control_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before uDWC_Test_Stand_Control is made visible.
function uDWC_Test_Stand_Control_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for uDWC_Test_Stand_Control
handles.output = hObject;

%loading settings and info into the program
p = fullfile(pwd, Info.DEFAULT_DIR, Info.LAST_FILE);
load(p,'info','-mat');
handles.info = info;
p = fullfile(pwd, Settings.DEFAULT_DIR, Settings.LAST_FILE);
load(p,'settings','-mat');
handles.settings = settings;

% creating dictionaries
handles.text_values = {...
    'handles.BoardProductEdit','handles.info.board_product';...
    'handles.BoardPartNumberEdit','handles.info.board_part_number';...
    'handles.BoardManufacturerEdit','handles.info.board_manufacturer';...
    'handles.BoardManufactureDateEdit','handles.info.board_manufacture_date';...
    'handles.BoardManufactureTimeEdit','handles.info.board_manufacture_time';...
    'handles.ProductNameEdit','handles.info.product_name';...
    'handles.ProductPartNumberEdit','handles.info.product_part_number';...
    'handles.ProductSerialEdit','handles.info.product_serial';...
    'handles.ProductManufacturerEdit','handles.info.product_manufacturer';...
    'handles.AuthorEdit','handles.info.meas_author';...
    'handles.LocationEdit','handles.info.meas_place';...
    'handles.ReportFilenameEdit','handles.info.report_file_name';...
    'handles.ReportDirectoryEdit','handles.info.report_file_location';...
    'handles.ReportStyleEdit','handles.info.report_style'...
    };
% 'handles.MeasDateEdit','handles.info.meas_date';...
% 'handles.MeasTimeEdit','handles.info.meas_time';...

handles.multitext_values = {...
    'handles.CoauthorsEdit','handles.info.meas_coauthors';...
    'handles.ReportExternalReferences','handles.info.report_external_ref'
    };

handles.numerical_values = {...
    'handles.BoardSerialEdit','handles.info.board_serial';...
    'handles.ProductVersionEdit','handles.info.product_version'...
    };

handles.boolean_values = {...
    'handles.SaveHumanReadableCheckbox','handles.info.report_human_readable';...
    'handles.ReadEEPROMCheckbox','handles.info.data_from_EEPROM';
    };

% Update handles structure
guidata(hObject, handles);

load_settings(hObject, handles);
load_info(hObject, handles);

% UIWAIT makes uDWC_Test_Stand_Control wait for user response (see UIRESUME)
% uiwait(handles.TestStandControlFigure);

% --- Outputs from this function are returned to the command line.
function varargout = uDWC_Test_Stand_Control_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in StartButton.
function StartButton_Callback(hObject, eventdata, handles)
% hObject    handle to StartButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(hObject,'enable','off');
error_info = apply_info(hObject, handles);
error_settings = apply_settings(hObject, handles);


if isempty(error_info) && isempty(error_settings)
%     try
        % set proper timing
        dt = clock();
        handles.info.meas_date = sprintf('%02d-%02d-%04d',dt(3), dt(2), dt(1));
        handles.info.meas_time = sprintf('%02d:%02d:%02d',dt(4), dt(5), floor(dt(6)));
        guidata(hObject,handles); %update changes
        % create proper directory structure
        p = handles.info.file_location();
        if exist(p,'file') == 0
            mkdir(p)
        end
        % create log file for the measurement
        measurement = Measurement(handles.settings, handles.info);
        measurement.measure(); % measure
        measurement.process(); % process
        measurement.evaluate(); % evaluate
        % save results
        p = fullfile(p, [handles.info.file_name() '__results.meas']);
        save(p, 'measurement');
        % generate report
        report = Report(handles.info.file_location(),p);
        report.generate(handles.info.report_style, handles.info.file_name());
        % clear memory
        handles.info.board_serial = 0; % clear serial numbers
        handles.info.product_serial = 'uDWC-18.9999';
        guidata(hObject,handles);
        load_info(hObject,handles); % apply changes in GUI
        % save last info and settings
        info = handles.info; 
        settings = handles.settings;
        save(fullfile(pwd, Info.DEFAULT_DIR, Info.LAST_FILE),'info');
        save(fullfile(pwd, Settings.DEFAULT_DIR, Settings.LAST_FILE),'settings');
%     catch error
%         msg = sprintf('Error occured during the measurement:\n%s',error.message);
%         h = msgbox(msg,'Error','error');
%         uiwait(h);
%     end
else
    h = msgbox('Please correct the values marked in red.','Error','error');
    uiwait(h);
end
set(hObject,'enable','on');

% --- Executes on button press in SettingsButton.
function SettingsButton_Callback(hObject, eventdata, handles)
% hObject    handle to SettingsButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Settings_GUI('uDWC_Test_Stand_Control'...
    ,handles.TestStandControlFigure, handles.settings);
guidata(hObject, handles);

% --- Executes on button press in HelpButton.
function HelpButton_Callback(hObject, eventdata, handles)
% hObject    handle to HelpButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in ExitButton.
function ExitButton_Callback(hObject, eventdata, handles)
% hObject    handle to ExitButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(handles.TestStandControlFigure);


function ReportFilenameEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ReportFilenameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ReportFilenameEdit as text
%        str2double(get(hObject,'String')) returns contents of ReportFilenameEdit as a double


% --- Executes during object creation, after setting all properties.
function ReportFilenameEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ReportFilenameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ReportDirectoryEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ReportDirectoryEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ReportDirectoryEdit as text
%        str2double(get(hObject,'String')) returns contents of ReportDirectoryEdit as a double


% --- Executes during object creation, after setting all properties.
function ReportDirectoryEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ReportDirectoryEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ReportStyleEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ReportStyleEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ReportStyleEdit as text
%        str2double(get(hObject,'String')) returns contents of ReportStyleEdit as a double


% --- Executes during object creation, after setting all properties.
function ReportStyleEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ReportStyleEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in SaveHumanReadableCheckbox.
function SaveHumanReadableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to SaveHumanReadableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of SaveHumanReadableCheckbox



% --- Executes on button press in ReadEEPROMCheckbox.
function ReadEEPROMCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to ReadEEPROMCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ReadEEPROMCheckbox
if get(hObject,'Value')
    enabled = 'off';
else
    enabled = 'on';
end
set(get(handles.BoardInfoPanel,'children'),{'enable'},{enabled});
set(get(handles.ProductInfoPanel,'children'),{'enable'},{enabled});


function AuthorEdit_Callback(hObject, eventdata, handles)
% hObject    handle to AuthorEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of AuthorEdit as text
%        str2double(get(hObject,'String')) returns contents of AuthorEdit as a double


% --- Executes during object creation, after setting all properties.
function AuthorEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to AuthorEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function CoauthorsEdit_Callback(hObject, eventdata, handles)
% hObject    handle to CoauthorsEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of CoauthorsEdit as text
%        str2double(get(hObject,'String')) returns contents of CoauthorsEdit as a double


% --- Executes during object creation, after setting all properties.
function CoauthorsEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CoauthorsEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function LocationEdit_Callback(hObject, eventdata, handles)
% hObject    handle to LocationEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of LocationEdit as text
%        str2double(get(hObject,'String')) returns contents of LocationEdit as a double


% --- Executes during object creation, after setting all properties.
function LocationEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LocationEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ProductNameEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ProductNameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ProductNameEdit as text
%        str2double(get(hObject,'String')) returns contents of ProductNameEdit as a double


% --- Executes during object creation, after setting all properties.
function ProductNameEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ProductNameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ProductPartNumberEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ProductPartNumberEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ProductPartNumberEdit as text
%        str2double(get(hObject,'String')) returns contents of ProductPartNumberEdit as a double


% --- Executes during object creation, after setting all properties.
function ProductPartNumberEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ProductPartNumberEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ProductVersionEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ProductVersionEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ProductVersionEdit as text
%        str2double(get(hObject,'String')) returns contents of ProductVersionEdit as a double


% --- Executes during object creation, after setting all properties.
function ProductVersionEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ProductVersionEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ProductSerialEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ProductSerialEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ProductSerialEdit as text
%        str2double(get(hObject,'String')) returns contents of ProductSerialEdit as a double


% --- Executes during object creation, after setting all properties.
function ProductSerialEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ProductSerialEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ProductManufacturerEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ProductManufacturerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ProductManufacturerEdit as text
%        str2double(get(hObject,'String')) returns contents of ProductManufacturerEdit as a double


% --- Executes during object creation, after setting all properties.
function ProductManufacturerEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ProductManufacturerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in RFFrequencyPopup.
function RFFrequencyPopup_Callback(hObject, eventdata, handles)
% hObject    handle to RFFrequencyPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns RFFrequencyPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from RFFrequencyPopup


% --- Executes during object creation, after setting all properties.
function RFFrequencyPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RFFrequencyPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in RFSourcePopup.
function RFSourcePopup_Callback(hObject, eventdata, handles)
% hObject    handle to RFSourcePopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns RFSourcePopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from RFSourcePopup


% --- Executes during object creation, after setting all properties.
function RFSourcePopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RFSourcePopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in LOSourcePopup.
function LOSourcePopup_Callback(hObject, eventdata, handles)
% hObject    handle to LOSourcePopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns LOSourcePopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from LOSourcePopup


% --- Executes during object creation, after setting all properties.
function LOSourcePopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOSourcePopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in CH0FunctionPopup.
function CH0FunctionPopup_Callback(hObject, eventdata, handles)
% hObject    handle to CH0FunctionPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns CH0FunctionPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from CH0FunctionPopup


% --- Executes during object creation, after setting all properties.
function CH0FunctionPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CH0FunctionPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function BoardProductEdit_Callback(hObject, eventdata, handles)
% hObject    handle to BoardProductEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of BoardProductEdit as text
%        str2double(get(hObject,'String')) returns contents of BoardProductEdit as a double


% --- Executes during object creation, after setting all properties.
function BoardProductEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BoardProductEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function BoardPartNumberEdit_Callback(hObject, eventdata, handles)
% hObject    handle to BoardPartNumberEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of BoardPartNumberEdit as text
%        str2double(get(hObject,'String')) returns contents of BoardPartNumberEdit as a double


% --- Executes during object creation, after setting all properties.
function BoardPartNumberEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BoardPartNumberEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function BoardSerialEdit_Callback(hObject, eventdata, handles)
% hObject    handle to BoardSerialEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of BoardSerialEdit as text
%        str2double(get(hObject,'String')) returns contents of BoardSerialEdit as a double


% --- Executes during object creation, after setting all properties.
function BoardSerialEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BoardSerialEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function BoardManufacturerEdit_Callback(hObject, eventdata, handles)
% hObject    handle to BoardManufacturerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of BoardManufacturerEdit as text
%        str2double(get(hObject,'String')) returns contents of BoardManufacturerEdit as a double


% --- Executes during object creation, after setting all properties.
function BoardManufacturerEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BoardManufacturerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function BoardManufactureDateEdit_Callback(hObject, eventdata, handles)
% hObject    handle to BoardManufactureDateEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of BoardManufactureDateEdit as text
%        str2double(get(hObject,'String')) returns contents of BoardManufactureDateEdit as a double


% --- Executes during object creation, after setting all properties.
function BoardManufactureDateEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BoardManufactureDateEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function BoardManufactureTimeEdit_Callback(hObject, eventdata, handles)
% hObject    handle to BoardManufactureTimeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of BoardManufactureTimeEdit as text
%        str2double(get(hObject,'String')) returns contents of BoardManufactureTimeEdit as a double


% --- Executes during object creation, after setting all properties.
function BoardManufactureTimeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BoardManufactureTimeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ReportExternalReferences_Callback(hObject, eventdata, handles)
% hObject    handle to ReportExternalReferences (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ReportExternalReferences as text
%        str2double(get(hObject,'String')) returns contents of ReportExternalReferences as a double


% --- Executes during object creation, after setting all properties.
function ReportExternalReferences_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ReportExternalReferences (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when the info is loaded from the file
function load_info(hObject, handles)

% setting numerical values and text values
dict = [handles.numerical_values; handles.text_values; handles.multitext_values];
for i = 1:length(dict)
    set(eval(dict{i,1}),'String'...
        , eval(dict{i,2}));
end

% setting boolean values
for i = 1:length(handles.boolean_values)
    set(eval(handles.boolean_values{i,1}),'Value'...
        , eval(handles.boolean_values{i,2}));
    eval([regexprep(handles.boolean_values{i,1},'handles\.','')...
        '_Callback(' handles.boolean_values{i,1} ', [], handles)']);
end
% clear the serial number values
set(handles.ProductSerialEdit,'String', 'uDWC-18.____');
set(handles.BoardSerialEdit,'String', '*');



function error = apply_info(hObject, handles)
error = [];
fields = findobj(handles.TestStandControlFigure,'Style','edit');
set(fields,{'BackgroundColor'},{[1 1 1]});
set(fields,{'TooltipString'},{''});

% read info form EEPROM if needed TODO


for i = 1:length(handles.numerical_values)
    try
        eval([handles.numerical_values{i,2} ' = str2double(get(' ...
            handles.numerical_values{i,1} ',''String''));']);
    catch error
        set(eval(handles.numerical_values{i,1}),'BackgroundColor',[1 0 0]);
        set(eval(handles.numerical_values{i,1}),'TooltipString',error.message);
    end
end

for i = 1:length(handles.text_values)
    try
        eval([handles.text_values{i,2} ' = get(' ...
            handles.text_values{i,1} ',''String'');']);
    catch error
        set(eval(handles.text_values{i,1}),'BackgroundColor',[1 0 0]);
        set(eval(handles.text_values{i,1}),'TooltipString',error.message);
    end
end

for i = 1:length(handles.multitext_values)
    try
        eval([handles.multitext_values{i,2} ' = get(' ...
            handles.multitext_values{i,1} ',''String'');']);
    catch error
        set(eval(handles.multitext_values{i,1}),'BackgroundColor',[1 0 0]);
        set(eval(handles.multitext_values{i,1}),'TooltipString',error.message);
    end
end

for i = 1:length(handles.boolean_values)
    eval([handles.boolean_values{i,2} ' = get(' ...
            handles.boolean_values{i,1} ',''Value'');']);
end



function load_settings(hObject, handles)
% setting the frequency popup
switch handles.settings.RF_frequency
    case 3.9
        set(handles.RFFrequencyPopup,'Value',3);
    case 3.0
        set(handles.RFFrequencyPopup,'Value',2);
    otherwise
        set(handles.RFFrequencyPopup,'Value',1);
end
%setting the RF source popup
if strcmp(handles.settings.RF_source,'backplane')
    set(handles.RFSourcePopup,'Value',2);
else
    set(handles.RFSourcePopup,'Value',1);
end
%setting the LO source popup
if strcmp(handles.settings.LO_source,'backplane')
    set(handles.LOSourcePopup,'Value',2);
else
    set(handles.LOSourcePopup,'Value',1);
end
%setting the CH0 function
if strcmp(handles.settings.RF_CH0_function,'RF')
    set(handles.CH0FunctionPopup,'Value',1);
else
    set(handles.CH0FunctionPopup,'Value',2);
end


function error = apply_settings(hObject, handles)
error = [];
fields = findobj(handles.TestStandControlFigure,'Style','popupmenu');
set(fields,{'BackgroundColor'},{[1 1 1]});
set(fields,{'TooltipString'},{''});

switch get(handles.RFFrequencyPopup,'Value')
    case 3
        handles.settings.RF_frequency = 3.9;
        handles.settings.LO_frequency = 3.954;
    case 2
        handles.settings.RF_frequency = 3;
        handles.settings.LO_frequency = 3.054;
    otherwise
        handles.settings.RF_frequency = 1.3;
        handles.settings.LO_frequency = 1.354;
end
%setting the RF source
if get(handles.RFSourcePopup,'Value') == 2
    handles.settings.RF_source = 'backplane';
else
    handles.settings.RF_source = 'frontpanel';
end
%setting the LO source
if get(handles.LOSourcePopup,'Value') == 2
    handles.settings.LO_source = 'backplane';
else
    handles.settings.LO_source = 'frontpanel';
end
%setting the CH0 function
if get(handles.CH0FunctionPopup,'Value') == 1
    handles.settings.RF_CH0_function = 'RF';
else
    handles.settings.RF_CH0_function = 'CLK';
end
